package com.example.li.day9_19_work_1;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

public class jindutiaoView extends View {

    private Paint paint;
    private float jindu;
    private float maxjindu;
    private int nColor;
    private int wColor;

    public jindutiaoView(Context context) {
        super(context);
    }

    public jindutiaoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        into(attrs);
        intoPaint();
    }

    private void intoPaint() {
        paint = new Paint();
        paint.setStrokeJoin(Paint.Join.ROUND);
    }

    private void into(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.jindutiaoView);
        jindu = typedArray.getFloat(R.styleable.jindutiaoView_jindu, 0);
        maxjindu = typedArray.getFloat(R.styleable.jindutiaoView_maxjindu, 100);
        nColor = typedArray.getColor(R.styleable.jindutiaoView_neiyuanColor, Color.BLUE);
        wColor = typedArray.getColor(R.styleable.jindutiaoView_waiyuanColor, Color.GREEN);
        typedArray.recycle();
    }

    private int width;
    private int height;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthPixels = getResources().getDisplayMetrics().widthPixels;
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        switch (modeWidth) {
            case MeasureSpec.EXACTLY:
                width=sizeWidth;
                break;
            case MeasureSpec.AT_MOST:
                width=widthPixels;
                break;
        }
        switch (modeHeight) {
            case MeasureSpec.EXACTLY:
                height=sizeHeight;
                break;
            case MeasureSpec.AT_MOST:
                height=widthPixels;
                break;
        }
        setMeasuredDimension(width,height);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        int min = Math.min(width, height);
        float a=50;
        float cx=width-a;
        float cy=height-a;
        float r=min/2-a;
        float laod;
        float nr=(r-10)*jindu/maxjindu;
        laod=360/maxjindu*jindu;
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.drawCircle(width/2,height/2,r,paint);

        paint.setStrokeWidth(20);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(wColor);
        canvas.drawArc(a,a,cx,cy,90,-laod,false,paint);

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(nColor);
        canvas.drawCircle(width/2,height/2,nr,paint);
    }

}
